package server

import (
	"src/zeus/iserver"
	"src/zeus/logger"
	"src/zeus/msgdef"
)

// SrvNet_Proc 处理网络消息的类
type SrvNet_Proc struct {
	srv *SrvNet
}

// 会话关闭,对应的会话连接断开的逻辑 ,不区分服务器客户端
func (proc *SrvNet_Proc) MsgProc_SessClosed(uid uint64) {
	srv := proc.srv

	ii, ok := srv.pendingSesses.LoadAndDelete(uid)
	if ok {
		if s, ok := ii.(iserver.ISess); ok {
			s.Close()
		}
		// srv.pendingSesses.Delete(uid)
		logger.Info("SessClose, remove from pending list ", uid)
	}

	ii, ok = srv.clientSesses.LoadAndDelete(uid)
	if ok {
		if s, ok := ii.(iserver.ISess); ok {
			s.Close()
		} else {
			logger.Warnf("clientSesses %+v is not iserver.ISess.", ii)

		}
		// ii.(iserver.ISess).Close()
		// srv.clientSesses.Delete(uid)
		logger.Info("SessClose, remove from client list ", uid)
	}

	logger.Info("SrvNet server ", srv.ServerID, " the connect with ", uid, " is closed")
}

// MsgProc_ClientVerifySucceedRet 连入其它服务器
// 我作为客户端,连接到其他服务器了
func (proc *SrvNet_Proc) MsgProc_ClientVerifySucceedRet(msg *msgdef.ClientVerifySucceedRet) {
	srv := proc.srv
	srv.onServerConnected(msg.SourceID, msg.Type)

	//seelogger.Debug("SrvNet server ", srv.srvID, " connect to sserver ", msg.SourceID, "  succeed")
}

// 连接到我的客户端 已经验证通过
func (proc *SrvNet_Proc) MsgProc_SessVerified(content interface{}) {
	srv := proc.srv
	im := content.(msgdef.ISessionVerifyReqMsg)
	uid := im.GetSessVerifyPID()
	ii := srv.IMsgServer.GetSession(uid)
	if ii != nil {
		_ = im.GetSource() == ii.GetType()
		srv.onClientConnected(ii)
	}

}
